<?php

class Core_Component_Account_Model_Role extends Soul_Model_Entity_Base {
	
	protected $tableName = '{t}account_role';
	
    public function validate() {
        if ($this->isNew()) {
            if ($this->checkExists(array('code'=>$this->getCode()))) {
            	throw new Core_Component_Account_Exception(Core_Component_Account_Exception::ROLE_REPEATED);
            }
        }
        return parent::validate();
    }
    
    protected function beforeInsert() {
        $this->updateLanguageFile();
        parent::beforeInsert();
    }
    
    protected function beforeUpdate(){
        $this->updateLanguageFile();
        parent::beforeUpdate();
    }
    
    private function updateLanguageFile(){
        $bus = App::getBusiness('system/localize');
        $bus->writeLanguageFile('account.role.'.$this->getCode(), $this->getName());
        $bus->writeLanguageFile('account.role.'.$this->getCode().'_desc', $this->getDescription());
        $this->setName('account.role.'.$this->getCode())
             ->setDescription('account.role.'.$this->getCode().'_desc');
    }
    
    protected function initializeProperties() {
        $this->createProperty('id', 'integer', TRUE, TRUE);
        $this->createProperty('code')
                ->setLabel('system.label.code')
                ->setUpdateable(FALSE)
                ->setRequired(TRUE)
                ->setInputType('textbox')
                ->addRule(new Soul_Validation_Rule_Character());
        $this->createProperty('name')
                ->setRequired(TRUE)
                ->setLabel('system.label.name')
                ->setInputType('textbox');
        $this->createProperty('description', 'string')
                ->setLabel('system.label.description')
                ->setInputType('textbox');
        $this->createProperty('status', 'string')
                ->setInputType(NULL)->setDefaultValue(CONSTANT_STATUS_ENABLED);        
    }

}